package com.example.submit.config.aop;

import com.example.submit.RepeatedSubmitUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 09948
 */
@Aspect
@Slf4j
@Component
public class SubmitAspect {

    @Resource
    private RepeatedSubmitUtil repeatedSubmitUtil;

    @Pointcut("@annotation(com.example.submit.config.aop.Submit)")
    public void pointCut() {
    }

    @Around("pointCut() && @annotation(submit)")
    public Object logRecord(ProceedingJoinPoint joinPoint, Submit submit) throws Throwable {
        Object proceed;
        try {
            repeatedSubmitUtil.lock("123");
            proceed = joinPoint.proceed();
        } finally {
            repeatedSubmitUtil.unlock("123");
        }

        return proceed;
    }
}
